package com.caotv.wkreport.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.caotv.wkreport.dao.Rooms;
import com.caotv.wkreport.dbutils.RoomsTableTool;
import com.caotv.wkreport.dbutils.UserTableTool;
import com.opensymphony.xwork2.ActionSupport;

public class RoomManagementAction extends ActionSupport{
	
	private List<Rooms> listRooms = null;
	private Rooms room = new Rooms();
	
	public String execute() {
		listRooms= RoomsTableTool.getInstance().getAllRooms();

		return "success";
	}

	public String editRoom(){
		System.out.println("\nRunning Edit Room Function!!!\n");
		HttpServletRequest request = ServletActionContext.getRequest();
		int id =Integer.parseInt(request.getParameter("id"));
		room = RoomsTableTool.getInstance().getRoomByID(id);
		return "success";
	}
	
	public String  doAddRoom() {
		System.out.println("\nRunning addRoom() Function!!!\n");
		HttpServletRequest request = ServletActionContext.getRequest();
		//int id =Integer.parseInt(request.getParameter("id"));
		int floor =Integer.parseInt(request.getParameter("floor"));
		String roomname =request.getParameter("roomname");
		int roomNumber =Integer.parseInt(request.getParameter("roomNumber"));
		String status = request.getParameter("status");
		boolean stas = true;

		if(status.compareToIgnoreCase("false")==0){
			stas = false;
		}
			
		String building =request.getParameter("building");
		
		System.out.println("Update Date: "+ " floor: "+ floor +"roomname :" + roomname +"roomNumber: "+roomNumber+"Building: "+building +"Status: "+status);
		
	//	room.setId(id);
		room.setFloor(floor);
		room.setRoomname(roomname);
		room.setRoomNumber(roomNumber);
		room.setStatus(stas);
		room.setBuilding(building);
		if(RoomsTableTool.getInstance().getRoomByName(roomname).size()>0)	{
			return "fail";
		}
		if(RoomsTableTool.getInstance().addRoom(room))	{
			return "success";
		}else {
			return "fail";
		}
	}
	
	public String deleteRoom(){
		System.out.println("\nRunning delete Room Function!!!\n");
		HttpServletRequest request = ServletActionContext.getRequest();
		int id =Integer.parseInt(request.getParameter("id"));
		RoomsTableTool.getInstance().deleteRoomByID(id);
		return "success";
	}
	
	public String saveOrUpdateRoom(){
		System.out.println("\nRunning saveOrUpdateRoom Function!!!\n");
		HttpServletRequest request = ServletActionContext.getRequest();
		int id =Integer.parseInt(request.getParameter("id"));
		int floor =Integer.parseInt(request.getParameter("floor"));
		String roomname =request.getParameter("roomname");
		int roomNumber =Integer.parseInt(request.getParameter("roomNumber"));
	//	String status =Integer.parseInt(request.getParameter("status"));
		String building =request.getParameter("building");
		
		System.out.println("Update Date: "+ id + " floor: "+ floor +"roomname :" + roomname +"roomNumber: "+roomNumber+"Building: "+building);
		
		room.setId(id);
		room.setFloor(floor);
		room.setRoomname(roomname);
		room.setRoomNumber(roomNumber);
		room.setStatus(true);
		room.setBuilding(building);
		if(RoomsTableTool.getInstance().getRoomByName(roomname).size()>0)	{
			return "fail";
		}
			
		RoomsTableTool.getInstance().saveOrUpdateRoom2(room);
		return "success";
	}
	
	public String addRoom(){
		return "success";
	}
	
	public Rooms getRoom() {
		return room;
	}

	public void setRoom(Rooms room) {
		this.room = room;
	}

	public List<Rooms> getListRooms() {
		return listRooms;
	}

	public void setListRooms(List<Rooms>listRooms) {
		this.listRooms = listRooms;
	}
}
